package com.xbongbong.paas.help.menu;

import com.xbongbong.admin.pojo.dto.MirrorRollBackDTO;
import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.domain.entity.PaasAppEntity;
import com.xbongbong.paas.domain.entity.PaasFormEntity;
import com.xbongbong.paas.domain.entity.PaasMenuEntity;
import com.xbongbong.paas.model.PaasMenuModel;
import com.xbongbong.paas.pojo.dto.MenuFormAddDTO;
import com.xbongbong.paas.service.PaasMenuService;
import com.xbongbong.paas.service.init.AbstractInitMenuHelp;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.pro.enums.AccessLinkEnum;
import com.xbongbong.pro.enums.SaasMarkEnum;
import com.xbongbong.pro.enums.menu.enums.FineClueMenuEnum;
import com.xbongbong.pro.enums.menu.enums.SearchCustomerMenuEnum;
import com.xbongbong.pro.enums.menu.enums.SystemAppMenuEnum;
import com.xbongbong.pro.menu.pojo.dto.MenuFormAddBatchDTO;
import com.xbongbong.pro.menu.pojo.dto.MenuInitDTO;
import com.xbongbong.pro.menu.pojo.vo.MenuInitVO;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author 吴峰
 * @date 2022/11/03 14:32
 */
@Component
public class InitSearchCustomerMenuHelp extends AbstractInitMenuHelp {
    @Resource
    private PaasMenuModel paasMenuModel;
    @Resource
    private PaasMenuService paasMenuService;

    @Override
    public List<String> alias() {
        return Arrays.asList(SystemAppMenuEnum.SEARCH_CUSTOMER.getAlias());
    }
    @Override
    public MenuInitVO initApp(MenuInitDTO menuInitDTO, Map<String, PaasAppEntity> appMap) throws XbbException {
        MenuFormAddBatchDTO fineAddBatchDTO = new MenuFormAddBatchDTO();
        BeanUtil.copyProperties(menuInitDTO, fineAddBatchDTO);
        Long appId = appMap.get(SystemAppMenuEnum.SEARCH_CUSTOMER.getAlias()).getId();
        fineAddBatchDTO.setAppId(appId);
        // 去掉旧菜单
        paasMenuModel.deleteByAlias(SearchCustomerMenuEnum.getAliasList(), menuInitDTO.getCorpid());
        // 初始化新菜单
        List<MenuFormAddDTO> fineMenuList = new ArrayList<>();
        for(FineClueMenuEnum fineClueMenuEnum: FineClueMenuEnum.values()){
            //如果已有该菜单，跳过循环
            PaasMenuEntity paasMenuEntity = paasMenuModel.getByAlias(fineClueMenuEnum.getAlias(), appId, menuInitDTO.getCorpid());
            if (Objects.nonNull(paasMenuEntity)){
                continue;
            }
            if(PaasConstant.HOME_STRING.equals(fineClueMenuEnum.getBelong())){
                MenuFormAddDTO menuFormAddDTO = new MenuFormAddDTO();
                BeanUtil.copyProperties(menuInitDTO, menuFormAddDTO);
                menuFormAddDTO.setAppId(appId);
                menuFormAddDTO.setParentId(0L);
                menuFormAddDTO.setSaasMark(SaasMarkEnum.SAAS.getCode());
                menuFormAddDTO.setAlias(fineClueMenuEnum.getAlias());
                menuFormAddDTO.setName(fineClueMenuEnum.getName());
                AccessLinkEnum accessLinkEnum = fineClueMenuEnum.getAccessLinkEnum();
                //取消精线索二级菜单图标
//                            menuFormAddDTO.setIcon(accessLinkEnum.getIcon());
                menuFormAddDTO.setColor(accessLinkEnum.getColor());
                menuFormAddDTO.setUrl(accessLinkEnum.getUrl());
                menuFormAddDTO.setMobileUrl(accessLinkEnum.getMobileUrl());
                menuFormAddDTO.setType(fineClueMenuEnum.getType());
                fineMenuList.add(menuFormAddDTO);
            }
        }
        fineAddBatchDTO.setMenuList(fineMenuList);
        paasMenuService.addBatchFormMenu(fineAddBatchDTO);
        return new MenuInitVO();
    }

    @Override
    public MenuInitVO initMenu(MenuInitDTO menuInitDTO, Map<String, PaasAppEntity> appMap) throws XbbException {
        return null;
    }

    @Override
    public void rollBackInit(MirrorRollBackDTO mirrorRollBackDTO, List<Long> installFormList, PaasFormEntity productFormEntity) throws XbbException {

    }
}
